\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}

\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0/}). This lab was written for OpenIntro by Andrew Bray and Mine \c{C}etinkaya-Rundel.}

\section*{Lab 4A: Foundations for statistical inference - Sampling distributions}
In this lab, we investigate the ways in which the statistics from a random sample of data can serve as point estimates for population parameters.  We're interested in formulating a \emph{sampling distribution} of our estimate in order to learn about the properties of the estimate, such as its distribution.

\subsection*{The data}
We consider real estate data from the city of Ames, Iowa.  The details of every real estate transaction in Ames is recorded by the City Assessor's office.  Our particular focus for this lab will be all residential home sales in Ames between 2006 and 2010.  This collection represents our population of interest.  In this lab we would like to learn about these home sales by taking smaller samples from the full population.  Let's load the data.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{download.file}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/ames.RData"}\hlstd{,} \hlkwc{destfile} \hlstd{=} \hlstr{"ames.RData"}\hlstd{)}

\hlkwd{load}\hlstd{(}\hlstr{"ames.RData"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


We see that there are quite a few variables in the data set, enough to do a very in-depth analysis.  For this lab, we'll restrict our attention to just two of the variables: the above ground living area of the house in square feet (\hlstd{Gr.Liv.Area}) and the sale price (\hlstd{SalePrice}).  To save some effort throughout the lab, create two variables with short names that represent these two variables.  

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{area} \hlkwb{<-} \hlstd{ames}\hlopt{$}\hlstd{Gr.Liv.Area}

\hlstd{price} \hlkwb{<-} \hlstd{ames}\hlopt{$}\hlstd{SalePrice}
\end{alltt}
\end{kframe}
\end{knitrout}


Let's look at the distribution of area in our population of home sales by calculating a few summary statistics and making a histogram.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(area)}

\hlkwd{hist}\hlstd{(area)}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Describe this population distribution.
\end{exercise}

\subsection*{The unknown sampling distribution}
In this lab we have access to the entire population, but this is rarely the case in real life.  Gathering information on an entire population is often extremely costly or impossible.  Because of this, we often take a sample of the population and use that to understand the properties of the population.

If we were interested in estimating the mean living area in Ames based on a sample, we can use the following command to survey the population.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{samp1} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


This command collects a simple random sample of size 50 from the vector \hlstd{area}, which is assigned to \hlstd{samp1}.  This is like going into the City Assessor's database and pulling up the files on 50 random home sales.  Working with these 50 files would be considerably simpler than working with all 2930 home sales.

\begin{exercise}
Describe the distribution of this sample? How does it compare to the distribution of the population?
\end{exercise}

If we're interested in estimating the average living area in homes in Ames using the sample, our best single guess is the sample mean.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{mean}\hlstd{(samp1)}
\end{alltt}
\end{kframe}
\end{knitrout}


Depending on which 50 homes you selected, your estimate could be a bit above or a bit below the true population mean of 1499.69 square feet.  In general, though, the sample mean turns out to be a pretty good estimate of the average living area, and we were able to get it by sampling less than 3\% of the population.

\begin{exercise}
Take a second sample, also of size 50, and call it \hlstd{samp2}.  How does the mean of \hlstd{samp2} compare with the mean of \hlstd{samp1}?  Suppose we took two more samples, one of size 100 and one of size 1000. Which would you think would provide a more accurate estimate of the population mean?
\end{exercise}

Not surprisingly, every time we take another random sample, we get a different sample mean.  It's useful to get a sense of just how much variability we should expect when estimating the population mean this way. The distribution of sample means, called the \emph{sampling distribution}, can help us understand this variability. In this lab, because we have access to the population, we can build up the sampling distribution for the sample mean by repeating the above steps many times. Here we will generate 5000 samples and compute the sample mean of each.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sample_means50} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{5000}\hlstd{)}

\hlkwa{for} \hlstd{(i} \hlkwa{in} \hlnum{1}\hlopt{:}\hlnum{5000}\hlstd{) \{}
    \hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
    \hlstd{sample_means50[i]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}
\hlstd{\}}

\hlkwd{hist}\hlstd{(sample_means50)}
\end{alltt}
\end{kframe}
\end{knitrout}


If you would like to adjust the bin width of your histogram to show a little more detail, you can do so by changing the \hlkwc{breaks} argument.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(sample_means50,} \hlkwc{breaks} \hlstd{=} \hlnum{25}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


Here we use R to take 5000 samples of size 50 from the population, calculate the mean of each sample, and store each result in a vector called \hlstd{sample\_means50}. On the next page, we'll review how this set of code works.

\begin{exercise}
How many elements are there in \hlstd{sample\_means50}?  Describe the sampling distribution, and be sure to specifically note its center.  Would you expect the distribution to change if we instead collected 50,000 sample means?
\end{exercise}

\subsection*{Interlude: The \texttt{for} loop}
Let's take a break from the statistics for a moment to let that last block of code sink in.  You have just run your first for loop, a cornerstone of computer programming.  The idea behind the for loop is \emph{iteration}: it allows you to execute code as many times as you want without having to type out every iteration.  In the case above, we wanted to iterate the two lines of code inside the curly braces that take a random sample of size 50 from \hlstd{area} then save the mean of that sample into the \hlstd{sample\_means50} vector.  Without the for loop, this would be painful:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sample_means50} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{5000}\hlstd{)}

\hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
\hlstd{sample_means50[}\hlnum{1}\hlstd{]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}

\hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
\hlstd{sample_means50[}\hlnum{2}\hlstd{]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}

\hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
\hlstd{sample_means50[}\hlnum{3}\hlstd{]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}

\hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
\hlstd{sample_means50[}\hlnum{4}\hlstd{]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}
\end{alltt}
\end{kframe}
\end{knitrout}


and so on...

With the for loop, these thousands of lines of code are compressed into a handful of lines. We've added one extra line to the code below, which prints the variable \hlstd{i} during each iteration of the for loop. Run this code.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sample_means50} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{5000}\hlstd{)}

\hlkwa{for} \hlstd{(i} \hlkwa{in} \hlnum{1}\hlopt{:}\hlnum{5000}\hlstd{) \{}
    \hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{50}\hlstd{)}
    \hlstd{sample_means50[i]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}
    \hlkwd{print}\hlstd{(i)}
\hlstd{\}}
\end{alltt}
\end{kframe}
\end{knitrout}


Let's consider this code line by line to figure out what it does.  In the first line we \emph{initialized a vector}.  In this case, we created a vector of 5000 zeros called \hlstd{sample\_means50}.  This vector will will store values generated within the for loop.

The second line calls the for loop itself.  The syntax can be loosely read as, ``for every element \hlstd{i} from 1 to 5000, run the following lines of code''. You can think of \hlstd{i} as the counter that keeps track of which loop you're on. Therefore, more precisely, the loop will run once when \hlstd{i}\hlopt{=}\hlnum{1}, then once when \hlstd{i}\hlopt{=}\hlnum{2}, and so on up to \hlstd{i}\hlopt{=}\hlnum{5000}.

The body of the for loop is the part inside the curly braces, and this set of code is run for each value of \hlstd{i}.  Here, on every loop, we take a random sample of size 50 from \hlstd{area}, take its mean, and store it as the \hlstd{i}\textsuperscript{th} element of \hlstd{sample\_means50}.

In order to display that this is really happening, we asked R to print \hlstd{i} at each iteration. This line of code is optional and is only used for displaying what's going on while the for loop is running.

The for loop allows us to not just run the code 5000 times, but to neatly package the results, element by element, into the empty vector that we initialized at the outset. 

\begin{exercise}
To make sure you understand what you've done in this loop, try running a smaller version.  Initialize a vector of 100 zeros called \hlstd{sample\_means\_small}.  Run a loop that takes a sample of size 50 from \hlstd{area} and stores the sample mean in \hlstd{sample\_means\_small}, but only iterate from 1 to 100.  Print the output to your screen (type \hlstd{sample\_means\_small} into the console and press enter).  How many elements are there in this object called \hlstd{sample\_means\_small}? What does each element represent?
\end{exercise}

\subsection*{Sample size and the sampling distribution}
Mechanics aside, let's return to the reason we used a for loop: to compute a sampling distribution, specifically, this one.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(sample_means50)}
\end{alltt}
\end{kframe}
\end{knitrout}


The sampling distribution that we computed tells us much about estimating the average living area in homes in Ames.  Because the sample mean is an unbiased estimator, the sampling distribution is centered at the true average living area of the the population, and the spread of the distribution indicates how much variability is induced by sampling only 50 home sales.

To get a sense of the effect that sample size has on our distribution, let's build up two more sampling distributions: one based on a sample size of 10 and another based on a sample size of 100.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sample_means10} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{5000}\hlstd{)}
\hlstd{sample_means100} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{5000}\hlstd{)}

\hlkwa{for} \hlstd{(i} \hlkwa{in} \hlnum{1}\hlopt{:}\hlnum{5000}\hlstd{) \{}
    \hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{10}\hlstd{)}
    \hlstd{sample_means10[i]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}
    \hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(area,} \hlnum{100}\hlstd{)}
    \hlstd{sample_means100[i]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}
\hlstd{\}}
\end{alltt}
\end{kframe}
\end{knitrout}


Here we're able to use a single for loop to build two distributions by adding additional lines inside the curly braces.  Don't worry about the fact that \hlstd{samp} is used for the name of two different objects.  In the second command of the for loop, the mean of \hlstd{samp} is saved to the relevant place in the vector \hlstd{sample\_means10}.  With the mean saved, we're now free to overwrite the object \hlstd{samp} with a new sample, this time of size 100.  In general, anytime you create an object using a name that is already in use, the old object will get replaced with the new one.

To see the effect that different sample sizes have on the sampling distribution, plot the three distributions on top of one another.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{par}\hlstd{(}\hlkwc{mfrow} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{3}\hlstd{,} \hlnum{1}\hlstd{))}

\hlstd{xlimits} \hlkwb{=} \hlkwd{range}\hlstd{(sample_means10)}

\hlkwd{hist}\hlstd{(sample_means10,} \hlkwc{breaks} \hlstd{=} \hlnum{20}\hlstd{,} \hlkwc{xlim} \hlstd{= xlimits)}
\hlkwd{hist}\hlstd{(sample_means50,} \hlkwc{breaks} \hlstd{=} \hlnum{20}\hlstd{,} \hlkwc{xlim} \hlstd{= xlimits)}
\hlkwd{hist}\hlstd{(sample_means100,} \hlkwc{breaks} \hlstd{=} \hlnum{20}\hlstd{,} \hlkwc{xlim} \hlstd{= xlimits)}
\end{alltt}
\end{kframe}
\end{knitrout}


The first command specifies that you'd like to divide the plotting area into 3 rows and 1 column of plots\symbolfootnote[2]{You may need to stretch your plotting window to accommodate the extra plots.  To return to the default setting of plotting one plot at a time, run the following command:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{par}\hlstd{(}\hlkwc{mfrow} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{1}\hlstd{,} \hlnum{1}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}


}. The \hlkwc{breaks} argument specifies the number of bins used in constructing the histogram.  The \hlkwc{xlim} argument specifies the range of the x-axis of the histogram, and by setting it equal to \hlstd{xlimits} for each histogram, we ensure that all three histograms will be plotted with the same limits on the x-axis.

\begin{exercise}
When the sample size is larger, what happens to the center?  What about the spread?
\end{exercise}

\vspace{1cm}

\subsection*{On your own}
So far, we have only focused on estimating the mean living area in homes in Ames.  Now you'll try to estimate the mean home price.

\begin{enumerate}

\item Take a random sample of size 50 from \hlstd{price}. Using this sample, what is your best point estimate of the population mean?

\item Since you have access to the population, simulate the sampling distribution for $\bar{x}_{price}$ by taking 5000 samples from the population of size 50 and computing 5000 sample means.  Store these means in a vector called \hlstd{sample\_means50}. Plot the data, then describe the shape of this sampling distribution. Based on this sampling distribution, what would you guess the mean home price of the population to be? Finally, calculate and report the population mean.

\item Change your sample size from 50 to 150, then compute the sampling distribution using the same method as above, and store these means in a new vector called \hlstd{sample\_means150}. Describe the shape of this sampling distribution, and compare it to the sampling distribution for a sample size of 50.  Based on this sampling distribution, what would you guess to be the mean sale price of homes in Ames?

\item Of the sampling distributions from 2 and 3, which has a smaller spread?  If we're concerned with making estimates that are more often close to the true value, would we prefer a distribution with a large or small spread?

\item What concepts from the textbook are covered in this lab?  What concepts, if any, are not covered in the textbook?  Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems?  Be specific in your answer.

\end{enumerate}


\end{document}
